package com.zzccyy.jpdemo;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import java.lang.ref.WeakReference;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author: lyman
 * @project: commomutil
 * @package: ly.commomutil
 * @date: 2019-07-13
 * @time: 10:12
 * @description:计时器（可以增长，也可以下降）
 */
public class ScheduleTimer {

    /**
     * 增长型
     */
    public static final int MODE_INCREASE = 1;
    /**
     * 下降型
     */
    public static final int MODE_DECREASE = 2;

    private Context context;

    private int mCount = 0;
    private int mode = MODE_INCREASE;

    private OnTimeCountListener mListener;

    private ExecutorService executorService;

    private MyHandler handler;

    private String tag = "";

    public void setOnTimeCountListener(OnTimeCountListener mListener) {
        this.mListener = mListener;
    }

    public interface OnTimeCountListener {
        /**
         * 计时器回调方法
         * @param count 时间计数
         * @param tag 标记
         */
        void onTimeCount(int count, String tag);
    }

    /**
     * 计时器构造方法
     *
     * @param context
     * @param mode    计时模式 1 增长  2 下降
     */
    public ScheduleTimer(Context context, int mode) {
        this.context = context;
        this.mode = mode;
        handler = new MyHandler(context);
    }

    /**
     * 以上一次结束的时间开始计时
     */
    public void start() {
        startTime(mCount);
    }

    /**
     * 开始倒计时
     *
     * @param startTime 起始时间
     */
    public void startTime(int startTime) {
        if (executorService == null) {
            mCount = startTime;
            executorService = new ScheduledThreadPoolExecutor(2);
            ((ScheduledThreadPoolExecutor) executorService).scheduleAtFixedRate(new Runnable() {
                @Override
                public void run() {
                    Message message = Message.obtain();
                    message.arg1 = mCount;
                    handler.sendMessage(message);
                    if (mode == MODE_INCREASE) {
                        mCount++;
                    } else {
                        mCount--;
                    }
                }

            }, 1, 1, TimeUnit.SECONDS);
        }
    }

    /**
     * 开始倒计时
     *
     * @param startTime 起始时间
     */
    public void startTime(int startTime,String tag) {
        if (executorService == null) {
            this.tag = tag;
            mCount = startTime;
            executorService = new ScheduledThreadPoolExecutor(2);
            ((ScheduledThreadPoolExecutor) executorService).scheduleAtFixedRate(new Runnable() {
                @Override
                public void run() {
                    Message message = Message.obtain();
                    message.arg1 = mCount;
                    handler.sendMessage(message);
                    if (mode == MODE_INCREASE) {
                        mCount++;
                    } else {
                        mCount--;
                    }
                }

            }, 1, 1, TimeUnit.SECONDS);
        }
    }

    /**
     * 结束倒计时
     */
    public void endTime() {
        if (executorService != null) {
            executorService.shutdown();
            executorService = null;
        }
    }

    public class MyHandler extends Handler {

        private WeakReference<Context> reference;

        public MyHandler(Context context) {
            this.reference = new WeakReference<>(context);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (reference.get() != null) {
                int count = msg.arg1;
                if (mListener != null) {
                    if (count < 0) {
                        endTime();
                    } else {
                        mListener.onTimeCount(count,tag);
                    }
                }
            }
        }
    }
}